/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.sales;

import com.kimstore.bean.Customer;
import com.kimstore.bean.Item;
import com.kimstore.bean.ItemStatus;
import com.kimstore.bean.Order;
import com.kimstore.bean.Staff;
import com.kimstore.factory.DaoFactory;
import com.kimstore.main.controller.OrdersTabController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ViewUpdateOrderController {

    private OrdersTabController parentController = null;
    private ViewUpdateOrderWindow window = null;
    private Integer orderId = null;
    private Order order = null;
    private List<Staff> staffs = null;
    private List<Customer> customers = null;
    private List<String> remove = new ArrayList<String>();
    private int originalSize = 0;

    public ViewUpdateOrderController(OrdersTabController parentController, Integer orderId) {
        this.parentController = parentController;
        this.orderId = orderId;

        staffs = DaoFactory.getInstance().getStaffDao().findAll();
        customers = DaoFactory.getInstance().getCustomerDao().findAll();
    }

    public void displayWindow() {
        order = DaoFactory.getInstance().getOrderDao().findById(orderId);
        originalSize = order.getItems().size();

        window = new ViewUpdateOrderWindow(this);
        window.populateStaffs(DaoFactory.getInstance().getStaffDao().findAll());
        window.populateCustomers(DaoFactory.getInstance().getCustomerDao().findAll());
        window.populateWindow(order);
        window.showWindow();
    }

    public void fromWindow() {
    }

    public void createViewUpdateItemController(String sku) {
        ViewUpdateItemController controller = new ViewUpdateItemController(this, sku);
        controller.displayWindow();
    }

    public void createViewUpdateItemControllerCallback() {
    }

    public void refreshDeliveryHistoryStatus() {
        order = DaoFactory.getInstance().getOrderDao().findById(orderId);
        window.refreshItemsTable(order);
        parentController.refreshTable();
    }

    public void addItemDeliveryHistoryStatus(List<String> skus, String status, String staffName) {
        Date today = new Date();

        Staff staff = DaoFactory.getInstance().getStaffDao().findByName(staffName);

        for (String sku : skus) {
            ItemStatus i = new ItemStatus();

            i.setStaff(staff);
            i.setDate(today);
            i.setType(status);
            i.setRemark("Updated via Quick Update.");

            Item item = getOrderItem(sku);
            item.addHistory(i);

            DaoFactory.getInstance().getItemDao().saveOrUpdate(item);
        }

        window.refreshItemsTable(order);
        parentController.refreshTable();
    }

    private Item getOrderItem(String sku) {
        for (Item i : order.getItems()) {
            if (i.getSerial().equals(sku)) {
                return i;
            }
        }
        return null;
    }

    void removeItem(String sku) {
        for (Item item : order.getItems()) {
            if (item.getSerial().equals(sku)) {
                order.getItems().remove(item);

                item.setPurchaseDetail(null);

                order.getVoidedItems().add(item);

                ItemStatus itemStatus = new ItemStatus();
                itemStatus.setDate(new Date());
                itemStatus.setType(ItemStatus.VOIDED);
                itemStatus.setRemark("Removed from Order");
                itemStatus.setStaff(null);
                item.addHistory(itemStatus);

                DaoFactory.getInstance().getItemDao().saveOrUpdate(item);

                break;
            }
        }

        DaoFactory.getInstance().getOrderDao().saveOrUpdate(order);
        //parentController.refreshTable();
        //parentController.parentController.getInventoryTabController().refreshTable();
    }

    public Item getItem(String sku) {
        for (Item item : order.getItems()) {
            if (item.getSerial().equals(sku)) {
                return item;
            }
        }
        return null;
    }

    void updateOrder(Staff staff, Customer customer, String customerType) {
        order.setStaff(staff);
        order.setCustomer(customer);
        order.setCustomerType(customerType);

        for (String string : remove) {
            removeItem(string);
        }

        DaoFactory.getInstance().getOrderDao().saveOrUpdate(order);

        parentController.refreshTable();
    }

    void deleteOrder() {
        for (Item item : order.getItems()) {
            item.setPurchaseDetail(null);
            DaoFactory.getInstance().getItemDao().saveOrUpdate(item);
        }

        DaoFactory.getInstance().getOrderDao().delete(order);

    }

    void refreshOrderTable() {
        parentController.refreshTable();
        parentController.parentController.getInventoryTabController().refreshTable();
        parentController.parentController.getCustomerTabController().refreshTable();
        parentController.parentController.getStaffTabController().refreshTable();
    }

    public void addToRemove(String sku) {
        remove.add(sku);
    }
}
